package com.wx.ut;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * This class echoes a string called from JavaScript.
 *
 * @author Administrator
 */
public class wxUt extends CordovaPlugin {

    @Override
    public boolean execute ( String action, JSONArray args, CallbackContext callbackContext ) throws JSONException {
        try {
            switch ( action ) {
                case "log":
                    log ( args.getString ( 0 ) );
                    return true;
                case "toast":
                    toast ( args.getString ( 0 ) );
                    return true;
                case "toWechat":
                    openWeChat ( cordova.getActivity ( ) );
                    return true;
                case "openAppletLink":
                    String username = args.getString ( 0 );
                    String path = args.getString ( 1 );
                    openAppletLink ( cordova.getActivity ( ), username, path );
                    return true;
                default:
                    return false;
            }
        } catch ( Exception e ) {
            android.util.Log.e ( "excc", e.getMessage ( ) );
            callbackContext.error ( e.getMessage ( ) );
            return false;
        }
    }

    private void log ( String s ) {
        android.util.Log.i ( "excc", s );
    }

    private void toast ( String s ) {
        Toast.makeText ( cordova.getContext ( ), s, Toast.LENGTH_SHORT ).show ( );
    }

    private void openWeChat ( Activity activity ) {
        log ( "正在打开微信" );
        Intent lan = activity.getPackageManager ( ).getLaunchIntentForPackage ( "com.tencent.mm" );
        Intent intent = new Intent ( Intent.ACTION_MAIN );
        intent.addCategory ( Intent.CATEGORY_LAUNCHER );
        intent.addFlags ( Intent.FLAG_ACTIVITY_NEW_TASK );
        intent.setComponent ( lan.getComponent ( ) );
        activity.startActivity ( intent );
    }

    public static final String APP_ID = "wxfa842d455a2c1100";
    private BroadcastReceiver broadcastReceiver;

    private void openAppletLink ( Activity activity, String username, String path ) {
        log ( "微信小程序用户名：" + username + ",路径：" + path );
        IWXAPI api = WXAPIFactory.createWXAPI ( activity, APP_ID, true );
        broadcastReceiver = new BroadcastReceiver ( ) {
            @Override
            public void onReceive ( Context context, Intent intent ) {
                log ( "将该app注册到微信----------------------" );
                // 将该app注册到微信
                api.registerApp ( APP_ID );
            }
        };
        activity.registerReceiver ( broadcastReceiver,
                new IntentFilter ( ConstantsAPI.ACTION_REFRESH_WXAPP ) );
        WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req ( );
        // 填小程序原始id
        req.userName = username;
        //拉起小程序页面的可带参路径，不填默认拉起小程序首页，对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"。
        req.path = path;
        // 可选打开 开发版，体验版和正式版
        req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;
        api.sendReq ( req );
    }

    @Override
    public void onDestroy ( ) {
        if ( broadcastReceiver != null ) {
            cordova.getActivity ( ).unregisterReceiver ( broadcastReceiver );
        }
        super.onDestroy ( );
    }
}